package com.message.rabbithandler;

import com.alibaba.fastjson.JSON;
import com.message.config.RabbitMqConfig;
import com.message.feignclient.MediaSearchClient;
import com.message.model.Media;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Arrays;

/**
 * @Author: 小何
 * @Description: TODO
 * @DateTime: 2023/10/25 16:41
 **/
@Component
public class RabbitHandler {

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    private final static String MEDIA_PRE = "media_";

    @Autowired
    private MediaSearchClient mediaSearchClient;

    //监听email队列
    @RabbitListener(queues = {RabbitMqConfig.QUEUE_INFORM_REDIS})
    public void receive_email(Object msg, Message message, Channel channel) {
        String mediaString = new String(message.getBody());
        Media media = JSON.parseObject(mediaString, Media.class);
        redisTemplate.opsForValue().set(MEDIA_PRE + media.getId(), mediaString);
    }

    //监听sms队列
    @RabbitListener(queues = {RabbitMqConfig.QUEUE_INFORM_ES})
    public void receive_sms(Object msg, Message message, Channel channel) {
        String mediaString = new String(message.getBody());
        mediaSearchClient.saveMedia(JSON.parseObject(mediaString, Media.class));
    }
}
